package com.smcdd.cdb.dbo.cdb_dbo.service.presenter;

import android.net.wifi.ScanResult;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.app.App;
import com.smcdd.cdb.dbo.cdb_dbo.network.HttpObserver;
import com.smcdd.cdb.dbo.cdb_dbo.network.factory.RetrofitFactory;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.BaseResponseEntity;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Simple;
import com.smcdd.cdb.dbo.cdb_dbo.service.entity.Statistics;
import com.smcdd.cdb.dbo.cdb_dbo.service.maneger.NetWorkCodeInfo;
import com.smcdd.cdb.dbo.cdb_dbo.service.view.IStatisticsAc;
import com.smcdd.cdb.dbo.cdb_dbo.ui.activity.StatisticsActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.TopAdapter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainEncry;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ObtainVersion;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ToastUtil;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.BiaogeDialog;
import com.smcdd.cdb.dbo.cdb_dbo.ui.widget.ListDialog;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 作者：Administrator on 2017/7/3 10:57
 * 邮箱：zhanghuaiha@gmail.com
 */

public class StatisticsPresenter extends BasePresenter<IStatisticsAc> {

    private StatisticsActivity ac;
    private TopAdapter topAdapter;
    private RecyclerView top;
    private TextView start;
    private TextView end;
    private BiaogeDialog selfDialog;
    private String versionCode;
    private String eid,id;
    private Simple scan;

    public StatisticsPresenter(BaseActivity context) {
        super(context);
    }

    public void loadContacts(String udid,String id){
        ac = getView().getAc();
        top = getView().getTop();
        start = getView().getStart();
        end = getView().getEnd();
        versionCode = ObtainVersion.getVersionName(mContext);
        eid = App.SP.getString("id", "");
        GridLayoutManager mgr=new GridLayoutManager(ac,5);
        top.setLayoutManager(mgr);
        this.id = id;
        setAdapter();
        loadNetWork(udid,start.getText().toString(),end.getText().toString());
    }

    private void setAdapter() {
        String[] stringArray = ac.getResources().getStringArray(R.array.hint);
        topAdapter = new TopAdapter(mContext, stringArray);
        top.setAdapter(topAdapter);
        topAdapter.setItemClickListenter((scan, position,view) -> {
            switch (position) {
                case 0:
                    if (view.isSelected()) {
                        view.setBackgroundResource(R.drawable.hint_bg);
                    }else {
                        view.setBackgroundResource(R.color.hint_one);
                    }
                    view.setSelected(!view.isSelected());
                    ac.visi(position);
                    break;
                case 1:
                    if (view.isSelected()) {
                        view.setBackgroundResource(R.drawable.hint_bg);
                    }else {
                        view.setBackgroundResource(R.color.hint_two);
                    }
                    view.setSelected(!view.isSelected());
                    ac.visi(position);
                    break;
                case 2:
                    if (view.isSelected()) {
                        view.setBackgroundResource(R.drawable.hint_bg);
                    }else {
                        view.setBackgroundResource(R.color.hint_three);
                    }
                    view.setSelected(!view.isSelected());
                    ac.visi(position);
                    break;
                case 3:
                    if (view.isSelected()) {
                        view.setBackgroundResource(R.drawable.hint_bg);
                    }else {
                        view.setBackgroundResource(R.color.hint_four);
                    }
                    view.setSelected(!view.isSelected());
                    ac.visi(position);
                    break;
                case 4:
                    if (view.isSelected()) {
                        view.setBackgroundResource(R.drawable.hint_bg);
                    }else {
                        view.setBackgroundResource(R.color.hint_five);
                    }
                    view.setSelected(!view.isSelected());
                    ac.visi(position);
                    break;
                case 5:
                    if (view.isSelected()) {
                        view.setBackgroundResource(R.drawable.hint_bg);
                    }else {
                        view.setBackgroundResource(R.color.hint_six);
                    }
                    view.setSelected(!view.isSelected());
                    ac.visi(position);
                    break;
                case 9:
                    ac.showDialog();
                    break;
                default:
                    break;
            }
        });
    }

    public void loadNetWork(String udid, String start_date, String end_date) {
        String time = System.currentTimeMillis() + "";
        String encry = ObtainEncry.getStatistics_store("2", time, versionCode, NetWorkCodeInfo.STATISTICS_STORE, udid, eid, id, start_date, end_date);
        RetrofitFactory.getmInstanse().mApi.getStatistics_store("2", time, versionCode, NetWorkCodeInfo.STATISTICS_STORE, udid, eid, id,start_date,end_date,encry)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseResponseEntity<List<Statistics>>>() {
                    @Override
                    public void success(BaseResponseEntity<List<Statistics>> listBaseResponseEntity) {
                        if (listBaseResponseEntity.code != 200) {
                            ToastUtil.showToast(listBaseResponseEntity.message);
                        }else {
                            List<Statistics> data = listBaseResponseEntity.data;
                            if (data.size() == 0) {
                                ToastUtil.showToast("未发现当前选中日期参数");
                            }else {
                                ac.setData(data);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        e.printStackTrace();
                    }
                });
    }


    public void Dialog() {
        selfDialog = new BiaogeDialog(mContext);
        selfDialog.show();
    }

    public void Dialog(String a,String b) {
        selfDialog = new BiaogeDialog(mContext);
        selfDialog.setTitle(a);
        selfDialog.setMessage(b);
        selfDialog.show();
    }
}
